﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Cfg;

namespace Ghy.Soft.Data
{
    public class FluentFactory
    {
        private static ISessionFactory sessionFactory { get; set; }

        private static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
              .Database(
                //SQLiteConfiguration.Standard.UsingFile("Ghy.Soft.db")
               MsSqlConfiguration.MsSql2005.ConnectionString(m=>m.Server("SDX-PC\\SQLSERVER").Username("sa").Password("0").Database("NDB"))
              )
              .Mappings(m =>m.FluentMappings.AddFromAssemblyOf<Ghy.Soft.Domain.Mappings.AdminMap>())
              .ExposeConfiguration(BuildSchema)
              .BuildSessionFactory();
        }

        private static void BuildSchema(Configuration config)
        {
            //new SchemaExport(config).Create(false, true);
        }

        public static ISession GetSession()
        {
            if (sessionFactory == null)
            {
                sessionFactory = CreateSessionFactory();
            }
            return sessionFactory.OpenSession();
        }
    }
}
